const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data: {
    getColor: getColor(),
    list: [100, 200, 400, 800, 1000, 2000],
    money: '',
    listItem: -1,
  },

  onLoad(){
    $wx.setNavColor(getColor().val); 
  },

  onShareAppMessage(){

  },
  
  //获取充值金额
  rechargeAmount(e){
    let currentTarget = e.currentTarget.dataset;

    this.setData({
      listItem: currentTarget.item,
      money: currentTarget.money
    });
  },
  
  //获取充值金额
  Input(){
    this.setData({
      listItem: -1
    });
  },

  formSubmit(e){
    let _this = this, form_obj = e.detail.value;

    if(!form_obj.money){
      $wx.showToast('请输入充值金额');
    }else{
      wx.showLoading({
        title: '充值中...',
        mask: true
      });

      AJAX.payRecharge({
        money: form_obj.money
      },(status, data, msg)=>{
        wx.hideLoading();
        if(status){
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
              $wx.showToast('支付成功');
            },
            fail(res) {
              $wx.showToast('支付失败');
            },
            complete() {
              setTimeout(callback => {
                wx.navigateBack();
              }, 2000);
            }
          });
        }else{
          $wx.showToast(msg);
        }
      });
    }
  },
  
});